/**
 * 
 */
package com.etc.admin.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.etc.admin.service.EmployeeService;
import com.etc.entity.Employee;
import com.etc.util.CommonMessage;
import com.etc.util.PageData;

/**
 * @author ccq
 *
 *         date: 2019年4月28日 下午5:58:57
 */
@Controller
@SessionAttributes(value = "adminlogin")
public class EmployeeController {
	@Resource(name = "employeeService")
	private EmployeeService employeeService;

	@RequestMapping(value = "listEmployee", method = RequestMethod.GET)
	@ResponseBody
	public PageData<Employee> listEmployee(Integer page, Integer limit, String content) {

		if (content == null) {
			content = "";
		}

		PageData<Employee> pd = employeeService.getEmployeeByPage(page, limit, content);

		// 这里返回json格式对象
		pd.setCode(0);
		pd.setMsg("请求成功");

		return pd;

	}

	@RequestMapping(value = "addEmployee", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage addEmployee(@RequestBody Employee e) {

		Employee employee = new Employee();
		employee.setEmpname(e.getEmpname());
		employee.setEmppwd(e.getEmppwd());
		employee.setEmpsex(e.getEmpsex());
		employee.setEmptel(e.getEmptel());
		employee.setEmplevel(e.getEmplevel());
		employee.setEmpaddress(e.getEmpaddress());

		// 这里返回json格式对象
		CommonMessage cm = new CommonMessage();

		boolean flag = employeeService.addEmployee(employee);

		if (flag) {
			cm.setMsg("添加成功");
		} else {
			cm.setMsg("添加失败");
		}

		return cm;

	}

	@RequestMapping(value = "updateEmployee", method = RequestMethod.PUT)
	@ResponseBody
	public CommonMessage updateEmployee(@RequestBody Employee e) {

		Employee employee = new Employee();
		employee.setEmpid(e.getEmpid());
		employee.setEmpname(e.getEmpname());
		employee.setEmppwd(e.getEmppwd());
		employee.setEmpsex(e.getEmpsex());
		employee.setEmptel(e.getEmptel());
		employee.setEmplevel(e.getEmplevel());
		employee.setEmpaddress(e.getEmpaddress());
		System.out.println(employee);
		System.out.println(e);

		// 这里返回json格式对象
		CommonMessage cm = new CommonMessage();

		boolean flag = employeeService.updateEmployee(employee);

		if (flag) {
			cm.setMsg("修改成功");
		} else {
			cm.setMsg("修改失败");
		}

		return cm;

	}

	/**
	 * 管理员登录（李添杭）
	 * 
	 * @param empName
	 * @param empPwd
	 * @return
	 */

	@RequestMapping(value = "getEmployee", method = RequestMethod.POST)
	@ResponseBody
	public CommonMessage getEmployee(String empname, String emppwd, HttpServletRequest request) {
		Employee employee = employeeService.selectEmpByEmpNameAndEmpPwd(empname, emppwd);
		/*System.out.println(employee);
		request.getSession().setAttribute("employee", employee);*/
		CommonMessage cm = new CommonMessage();
		if (employee != null) {
			cm.setMsg("登入成功");
			/* cm.setCode(0); */
			request.getSession().setAttribute("employee", employee);
			return cm;
		}
		cm.setMsg("登录失败");
		return cm;
	}
}
